package com.kamloo.stg.data.db;

import java.util.ArrayList;

import com.kamloo.stg.data.db.MySQLiteHelper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class LineDAO {
	private SQLiteDatabase m_database;
	private MySQLiteHelper m_dbHelper;
	public static final String TABLE_NAME 		 = "line";
	public static final String COLUMN_ID 		 = "_id";
	public static final String COLUMN_NAME 		 = "name";
	public static final String COLUMN_IDENTIFIER = "identifier";	
	private String[] m_allColumns = { COLUMN_ID, COLUMN_NAME, COLUMN_IDENTIFIER };
	
	public static final String TABLE_CREATE = 
			"CREATE TABLE " + 
		    TABLE_NAME + "(" + 
		    COLUMN_ID + " INTEGER PRIMARY KEY, " + 
		    COLUMN_NAME	+ " TEXT NOT NULL, " + 
		    COLUMN_IDENTIFIER	+ " TEXT NOT NULL);";
	
	public LineDAO(Context context) {
		m_dbHelper = new MySQLiteHelper(context);
	}

	public void open() throws SQLException {
		m_database = m_dbHelper.getWritableDatabase();
	}

	public void close() {
		m_dbHelper.close();
	}
		
	
	public void add(Line line) {
		ContentValues values = new ContentValues();
		values.put(COLUMN_ID, line.getId());
		values.put(COLUMN_NAME, line.getName());
		values.put(COLUMN_IDENTIFIER, line.getIdentifier());
		m_database.insert(TABLE_NAME, null, values);
	}
	
	public void delete(int id) {
		System.out.println("Line deleted with id: " + id);
		m_database.delete(TABLE_NAME, COLUMN_ID + " = " + id, null);
	}
	
	public Line select(int id) {
		Cursor cur = m_database.query(TABLE_NAME, m_allColumns, COLUMN_ID + " = " + id, 
									  null, null, null, null);
		
		if (cur != null)
			cur.moveToFirst();
		
		Line newLine = cursorToLine(cur);
		cur.close();
		return newLine;
	}
	
	public ArrayList<Line> selectAll(){
		ArrayList<Line> lines = new ArrayList<Line>();
		
		Cursor cur = m_database.query(TABLE_NAME, m_allColumns, 
				  					  null, null, null, null, null);

		if (cur != null) {
			cur.moveToFirst();
			while (!cur.isAfterLast()) {
				lines.add(cursorToLine(cur));
				cur.moveToNext();
			}
		}

		cur.close();
		return lines;
	}
	
	public void update(Line line) {
		ContentValues values = new ContentValues();
		values.put(COLUMN_NAME, line.getName());
		values.put(COLUMN_IDENTIFIER, line.getIdentifier());
		m_database.update(TABLE_NAME, values, COLUMN_ID + " = " + line.getId(), null);
	}	
	
	private Line cursorToLine(Cursor cursor) {
		return new Line(cursor.getInt(0),
						cursor.getString(1),
						cursor.getString(2));
	}
		
	/*
	public static Line selectFromHoraire(int id_Horaire, SQLiteDatabase db) {
		Line l = null;
		
		String query = String.format(SELECT_HORAIRE, COL_ID, COL_NOM, TABLE_NAME, 
									DirectionDAO.TABLE_NAME, COL_ID, DirectionDAO.COL_ID_LIGNE, 
									NoeudDAO.TABLE_NAME, DirectionDAO.COL_ID, NoeudDAO.COL_ID_DIRECTION,
									HoraireDAO.TABLE_NAME, HoraireDAO.COL_ID_NOEUD, NoeudDAO.COL_ID,
									HoraireDAO.COL_ID);
		
		Cursor cur = db.rawQuery(query, new String[] { String.valueOf(id_Horaire) });
		
		if (cur != null && cur.moveToFirst()) {
			l = new Line(cur.getInt(cur.getColumnIndex(COL_ID)),
						  cur.getString(cur.getColumnIndex(COL_NOM)));
		}
		
		return l;
	}	
		
	public static Arret GetArretDepart(SQLiteDatabase db, int lineId)
	{			
		ArrayList<Direction> directions = DirectionDAO.selectBeginEnd(lineId, db);
		return ArretDAO.select(directions.get(0).getId_arret(), db);
	}
	
	public static Arret GetArretFin(SQLiteDatabase db, int lineId)
	{
		ArrayList<Direction> directions = DirectionDAO.selectBeginEnd(lineId, db);
		return ArretDAO.select(directions.get(directions.size()-1).getId_arret(), db);
	}
	*/
}
